package com.hz.config;

import com.hz.pojo.Advertising;
import com.hz.service.AdvertisingService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class AdvertisingConfig {

    //把广告浏览量存入Redis 未完成
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private AdvertisingService advertisingService;

    @Bean
    public void getAll(){
        try {
            //获取广告集合
            List<Advertising> advertisingList = advertisingService.getAll();
            System.out.println(advertisingList);
            //循环遍历集合  获取id和浏览量，以键值对形式存储到Redis
            for (Advertising advertising : advertisingList) {
                advertising.getAdvertising_id(); //获取广告id
                Long traffic = advertising.getAdvertising_traffic(); //获取广告浏览量
                //存到Redis
                stringRedisTemplate.boundValueOps(advertising.getAdvertising_id()+"").increment(advertising.getAdvertising_traffic());
                //stringRedisTemplate.opsForValue().set(advertising.getAdvertising_id()+"",advertising.getAdvertising_traffic()+"");
            }
            /*//把集合转换为JSON字符串
            String advertisingJSON = JSON.toJSONString(advertisingList);
            //定义一个token
            String AdvertisingToken = "AdvertisingToken";
            //存入Redis
            stringRedisTemplate.opsForValue().set(AdvertisingToken,advertisingJSON);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
